package Server;


import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.filter.util.ReferenceCountingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import Filter.TestFilter;
import Handler.ServerOrderHandler;


public class Server {

	
	private int PORT;
	private IoAcceptor orderacceptor;
	
	public Server()
	{
		this.PORT = 8093;
		this.orderacceptor = new NioSocketAcceptor();
	}
	
	public Server(int port)
	{
	    this.PORT = port;
	    this.orderacceptor = new NioSocketAcceptor();
	}
	
	public void StartServer() throws Exception
	{
		this.orderacceptor.getFilterChain().addLast("loggor",new LoggingFilter());
		this.orderacceptor.getFilterChain().addLast(
				"codec",
				new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))
				);
	   // this.orderacceptor.getFilterChain().addLast("TestFilter", new ReferenceCountingFilter(new TestFilter()));
		
		this.orderacceptor.setHandler(new ServerOrderHandler());
		this.orderacceptor.setDefaultLocalAddress(new InetSocketAddress(this.PORT));
		this.orderacceptor.bind();
	}
	
//	public static void main(String[] args) throws IOException
//	{
//		IoAcceptor orderacceptor = new NioSocketAcceptor();
//			
//		orderacceptor.getFilterChain().addLast("loggor",new LoggingFilter());
//		orderacceptor.getFilterChain().addLast(
//				"codec",
//				new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))
//				);
//		
//		ServerOrderHandler sh = new ServerOrderHandler();
//		orderacceptor.setHandler(sh);
//		
////		orderacceptor.setDefaultLocalAddress(new InetSocketAddress(PORT));
//		
//		orderacceptor.bind();
//	}
}
